#include "GlobalHead.h"
#include "Gui/View.h"


#include "Gui/Gui_Application.h"
#include "Gui_Document.h"
#include "App/Document.h"
//#include "app/App_Application.h"
TYPESYSTEM_SOURCE_ABSTRACT(Gui::BaseView,Base::BaseClass);
//using namespace Gui;


Gui::BaseView::BaseView( Gui::Gui_Document* pcDocument)
:_pcDocument(pcDocument), bIsDetached(false)
{
	if (pcDocument){
		pcDocument->attachView(this);
		bIsPassive = false;
	}
	else{
		Gui_Application::instance->attachView(this);
		bIsPassive = true;
	}
}

Gui::BaseView::~BaseView(void)
{
}

void Gui::BaseView::deleteSelf()
{
  delete this;
}
App::Document* Gui::BaseView::getAppDocument() const
{
   return NULL;
}


void Gui::BaseView::onClose(void)
{
	if (bIsDetached)
		return;

	if (bIsPassive) {
		Gui_Application::instance->detachView(this);
		if (_pcDocument)
			_pcDocument->detachView(this, true);
	}
	else {
		if (_pcDocument)
			_pcDocument->detachView(this);
	}

	_pcDocument = 0;
	bIsDetached = true;
}

void Gui::BaseView::setDocument(Gui::Gui_Document* pcDocument)
{
	if (_pcDocument == pcDocument)
		return;
	// detaches and attaches the observer
	if (_pcDocument)
		_pcDocument->detachView(this, true);
	if (pcDocument)
		pcDocument->attachView(this,true);	

	// set the new document as the active one
	_pcDocument = pcDocument;
}

